package LRU;

import java.util.LinkedHashMap;
import java.util.Map;
//依赖JDK，LinkedHashMap投机取巧实现LRU
public class LRUCacheDemo<K,V>extends LinkedHashMap<K,V> {

    private int capacity;

    public LRUCacheDemo( int capacity) {
        //accessOrder确保插入有序
        super(capacity,0.75F,true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return super.size()>capacity;
    }

    public static void main(String[] args) {
        LRUCacheDemo<Object, Object> LRUCacheDemo = new LRUCacheDemo<>(3);
        LRUCacheDemo.put(1,"a");
        LRUCacheDemo.put(2,"b");
        LRUCacheDemo.put(3,"c");
        System.out.println(LRUCacheDemo.keySet());
    }
}
